package com.sbdev.comverter;

import com.sbdev.dic.SexEnum;
import com.sbdev.pojo.User;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

@Component
public class UserConverter implements Converter<String, User> {


    /**
     * 约定提交格式“{id}-{userName}-{sex}-{note}”
     * @param source 源字符串
     * @return 用户对象
     */
    @Override
    public User convert(String source) {
        if (source == null) {
            return null;
        }
        String []arr = source.split("-");
        if (arr.length != 4) { // 不符合格式
            return null;
        }
        User user = new User();
        user.setId(Long.parseLong(arr[0]));
        user.setUserName(arr[1]);
        user.setSex(SexEnum.getSexEnum(Integer.parseInt(arr[2])));
        user.setNote(arr[3]);
        return user;
    }
}
